import 'package:flutter/material.dart';
import 'dialog_manager.dart';

class LoadingDialog extends Dialog {
  final String text;
  final BuildContext buildContext;

  //click the back key to close or not
  final bool dismissOnBackPressed;

  LoadingDialog({@required this.buildContext, this.text, this.dismissOnBackPressed = true});

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        if (dismissOnBackPressed) {
          DialogManager.resetLoadingDialog();
          return true;
        } else {
          return false;
        }
      },
      child: Material(
        type: MaterialType.transparency,
        child: Center(
          child: SizedBox(
            width: 90,
            height: 90,
            child: Container(
              decoration:
                  BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(10)),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  CircularProgressIndicator(),
                  Container(margin: EdgeInsets.only(top: 15), child: Text(text.toString())),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  void dismissDialog() {
    Navigator.of(buildContext).pop();
  }
}
